package main

import "fmt"

type TreeNode struct {
	Val int
	Left *TreeNode
	Right *TreeNode
}

func createTree()*TreeNode{
	Tree1:=&TreeNode{}
	Tree2:=&TreeNode{}
	Tree3:=&TreeNode{}
	Tree4:=&TreeNode{}
	Tree5:=&TreeNode{}
	Tree6:=&TreeNode{}
	Tree7:=&TreeNode{}
	Tree1.Val=1
	Tree2.Val=2
	Tree3.Val=3
	Tree4.Val=4
	Tree5.Val=5
	Tree6.Val=6
	Tree7.Val=7
	Tree1.Left=Tree2
	Tree1.Right=Tree3
	Tree2.Left=Tree4
	Tree3.Left=Tree5
	Tree3.Right=Tree6
	Tree6.Left=Tree7
	return Tree1
}

func inOreder(root *TreeNode){
	if root==nil{
		return
	}
	inOreder(root.Left)
	fmt.Println(root.Val)
	inOreder(root.Right)
}
func traveseWithChannel(root *TreeNode)chan *TreeNode{
	out:=make(chan *TreeNode)
	go func() {
		queue:=make([]*TreeNode,0)
		queue=append(queue,root)
		for len(queue)!=0{
			out<-queue[0]
			if queue[0].Left!=nil{
				queue=append(queue,queue[0].Left)
			}
			if queue[0].Right!=nil{
				queue=append(queue,queue[0].Right)
			}
			queue=queue[1:]
		}
		close(out)
	}()
	return out
}

func main() {
	root:=createTree()
	c:=traveseWithChannel(root)
	for v:=range c{
		fmt.Println(v.Val)
	}
}
